//JS
const app = getApp()
const db = wx.cloud.database();
Page({
  data: {
    navbar: ["待收货", "待评价", "已完成", "已取消"],
    // 默认选中菜单
    currentTab: 0,
    orderStatus: 0, //-1订单取消,0新下单发货,1已收货待评价,2订单已完成
    list: []
  },
  //顶部tab切换
  navbarTap: function (e) {
    let index = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: index,
      orderStatus: index == 3 ? -1 : index
    })
    this.getMyOrderList();
  },

  onShow: function () {
    this.getMyOrderList();
  },

  getMyOrderList() {
    let user = wx.getStorageSync('user')
    wx.request({
      url: 'http://localhost:8080/api/orders/myOrder',
      method: 'POST',
      data: {
        action: 'user',
        userid: user.id,
        orderStatus: this.data.orderStatus
      },
      success: (res) => {
        console.log("我的订单列表", res)
        this.setData({
          list: res.data.data
        })
      }
    })
  },

  //去评论页面
  goCommentPage() {
    wx.navigateTo({
      url: '../myComment/myComment',
    })
  },
  // 去评论
  goComment(event) {
    console.log(event)
    wx.navigateTo({
      url: '/pages/comment/comment?id=' + event.currentTarget.dataset.item,
    })
  },


  //确认收货
  shouhuo(event) {
    let orderId = event.currentTarget.dataset.item.id;
    wx.request({
      url: 'http://localhost:8080/api/orders/receiving',
      method: 'POST',
      data: {
        id: orderId,
        status: 1
      },
      success: (res) => {
        if (res.data.code ==200) {
          console.log('确认收货成功', res)
          this.getMyOrderList()
          wx.showToast({
            title: '收货成功'
          })
        }else{
          console.log('参数异常', res)
          this.getMyOrderList()
          wx.showToast({
            title: res.data.msg
          })
        }
      
      },
      fail: (res) => {
        console.log('确认收货失败', res)
        wx.showToast({
          icon: 'none',
          title: res.data.msg
        })
      }
    })

  },
  //取消订单
  cancleOrder(event) {
    let item = event.currentTarget.dataset.item
    console.log(event)
    wx.request({
      url: 'http://localhost:8080/api/orders/cancel',
      method: 'POST',
      data: {
        ordersId: item.id,
        goodsId: item.goodsId,
        quantity: item.quantity,
        status: -1
      },
      success: (res) => {

        if (res.data.code == 200) {
          console.log('取消订单成功', res)
          wx.showToast({
            title: '取消订单成功',
          })
          this.getMyOrderList()
        } else {
          console.log('添加商品数量失败', res)
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }

      },
      fail: (res) => {
        console.log('取消订单失败', res)
        wx.showToast({
          icon: 'none',
          title: '取消订单失败',
        })
      }

    })
  },
  // 给卖家打电话
  call(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  //去商品详情页
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?goodid=' + e.currentTarget.dataset.goodid
    })
  },
})